package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.SendInfoBean;
import com.project.mapper.SendInfoMapper;
import com.project.service.ISendInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 收货信息表（用户模块） 服务实现类
 * </p>
 *
 * @author lhy
 * @since 2021-10-05
 */
@Service
@Transactional
public class SendInfoServiceImpl implements ISendInfoService {
    @Autowired
    private SendInfoMapper infoMapper;

    /**
     * 添加收货信息：地址，联系人，联系人性别，联系人电话，
     * 手机号码；（本系统以单个学校周边的商铺作为参考，范围不大，所以直接由用户添加收货地址）
     * 如果用户添加的是首个地址，那么将其（默认收货地址外键）修改为1
     * 1. 先验证用户名下的地址是否超过数量上限（设置数量上限为10），如果超过则添加失败
     * 2. 系统在该用户的名下添加新的地址（新地址用户外键设置为该用户id）
     *
     * @return 添加地址是否成功
     */
    public boolean addSendInfo(SendInfoBean sendInfoBean) {
        QueryWrapper<SendInfoBean> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", sendInfoBean.getUserId());
//        System.out.println(sendInfoBean.getUserId());
        Integer num = infoMapper.selectCount(wrapper);
        if (num == null || num > 10 || num < 0) {
            return false;
        }
        if (num == 0) {
            infoMapper.addSendInfo(sendInfoBean);
            //将默认地址外键设为1
            SendInfoBean sendInfoBean1 = infoMapper.selectById(sendInfoBean.getSendInfoId());
            sendInfoBean1.setDefaultStatus(1);
            QueryWrapper<SendInfoBean> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("sendInfoId", sendInfoBean1.getSendInfoId());
            infoMapper.update(sendInfoBean1, wrapper1);
            return true;
        }
        infoMapper.addSendInfo(sendInfoBean);
        return true;
    }

    @Override
    public boolean updateSendInfo(Integer sendInfoId, String newAddress, String newReceiver, String newGentle, String newPhone) {
        QueryWrapper<SendInfoBean> wrapper = new QueryWrapper<>();
        wrapper.eq("sendInfoId", sendInfoId);
        SendInfoBean sendInfoBean = new SendInfoBean();
        sendInfoBean.setSendInfoId(sendInfoId);
        sendInfoBean.setAddress(newAddress);
        sendInfoBean.setReceiver(newReceiver);
        sendInfoBean.setGentle(newGentle);
        sendInfoBean.setPhone(newPhone);
        infoMapper.update(sendInfoBean, wrapper);
        return true;
    }

    @Override
    public Integer defaultSendInfo(Integer defaultSendInfoId, Integer userId) {
//        QueryWrapper<SendInfoBean> wrapper = new QueryWrapper<>();

        infoMapper.defaultSendInfo(defaultSendInfoId, userId);
        return defaultSendInfoId;
    }

    @Override
    public List<SendInfoBean> findByUserId(int userId) {
        QueryWrapper<SendInfoBean> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        return infoMapper.selectList(wrapper);
    }

    @Override
    public SendInfoBean findById(int sendInfoId) {
        return infoMapper.selectById(sendInfoId);
    }
}
